<template>
  <section>
    <!--工具栏-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="condition">
        <el-form-item>
          <el-input v-model="condition.RY_degsonid" placeholder="人员工号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="condition.RY_name" placeholder="人员姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="condition.RY_department" placeholder="部门"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="condition.SS_number" placeholder="房间号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!-- 操作栏位 -->
    <el-col>
      <el-form :inline="true">
        <!-- <el-form-item>
          <el-button type="primary" @click="outExcel" size="small">导出EXCEL</el-button>
        </el-form-item>-->
        <el-form-item>
          <!-- <el-button type="success" @click="updateInfo" size="small">更新工号部门</el-button> -->
        </el-form-item>
      </el-form>
    </el-col>
    <!--数据展示界面-->
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="selsChange"
      v-loading="loading"
    >
      <el-table-column align="center" type="selection" fixed width="50"></el-table-column>
      <el-table-column align="center" label="序号" fixed type="index" width="50"></el-table-column>
      <el-table-column align="center" label="区域名称" prop="AR_name" fixed min-width="150"></el-table-column>
      <!-- <el-table-column align="center" label="区域编码" prop="AR_number" fixed min-width="80"></el-table-column> -->
      <el-table-column align="center" label="宿舍楼名称" prop="SSL_name" min-width="150"></el-table-column>
      <!-- <el-table-column align="center" label="宿舍楼编码" prop="SSL_number" fixed min-width="100"></el-table-column> -->
      <el-table-column align="center" label="楼栋号" prop="SSL_cavenumber" min-width="70"></el-table-column>
      <el-table-column align="center" label="楼层" prop="SSL_floor" min-width="50"></el-table-column>
      <el-table-column align="center" label="房间号" prop="SS_number" min-width="100"></el-table-column>
      <el-table-column align="center" label="房间类型" prop="SS_roomtype" min-width="100">
        <template slot-scope="scope">
          <div v-if="scope.row.SS_roomtype === '0'">单人间</div>
          <div v-else-if="scope.row.SS_roomtype ==='1' ">多人间</div>
          <div v-else-if="scope.row.SS_roomtype ==='2' ">公共房间</div>
          <div v-else></div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="性别" prop="SS_dormsex" min-width="50">
        <template slot-scope="scope">
          <div v-if="scope.row.SS_dormsex ==='1' ">男</div>
          <div v-else-if="scope.row.SS_dormsex ==='0'">女</div>
          <div v-else></div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="床位号" prop="RY_bed" min-width="100"></el-table-column>

      <el-table-column align="center" label="默认床位状态" prop="RY_bedstatus" min-width="100">
        <template slot-scope="scope">
          <div v-if="scope.row.RY_bedstatus === '0' ">禁用</div>
          <div v-else-if="scope.row.RY_bedstatus ==='1' ">启用</div>
          <div v-else></div>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="床位状态" prop="RY_bedstatus" min-width="80"></el-table-column> -->
      <el-table-column align="center" label="人员姓名" prop="RY_name" min-width="100"></el-table-column>
      <el-table-column align="center" label="工号" prop="RY_degsonid" min-width="100"></el-table-column>
      <el-table-column align="center" label="部门" prop="RY_department" min-width="100"></el-table-column>
      <el-table-column align="center" label="电话" prop="RY_phone" min-width="110"></el-table-column>
      <el-table-column fixed="right" align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="small" icon="el-icon-edit" @click="edit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="small" type="success" @click="changeBerth(scope.$index, scope.row)">更换床位</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :total="totalPage"
        style="float:right;"
      ></el-pagination>
    </el-col>

    <!-- 编辑 -->
    <el-dialog title="编辑" :visible.sync="alterFormVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="bed_update" :model="bed_update">
        <el-form-item label="姓名">
          <el-input v-model="bed_update.RY_name" disabled></el-input>
        </el-form-item>
        <el-form-item label="房间号">
          <el-input v-model="bed_update.SS_number" disabled></el-input>
        </el-form-item>
        <el-form-item label="床位">
          <el-input v-model="bed_update.RY_bed" disabled></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="bed_update.RY_phone" :disabled="alterTelControl"></el-input>
        </el-form-item>
        <!-- <el-form-item label="入住时间">
          <el-input v-model="bed_update.inTime" disabled></el-input>
        </el-form-item>-->
        <el-form-item label="默认床位状态">
          <el-radio-group v-model="bed_update.RY_bedstatus" :disabled="berthTypeControl">
            <el-radio label="1">启用</el-radio>
            <el-radio label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="bed_update.RY_remarks" type="textarea" :rows="4" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="领用物品">
          <el-transfer v-model="goods" :data="data" :titles="['可领用','已领用']"></el-transfer>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="alterFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="alterSubmit" :loading="alterLoading">提交</el-button>
      </div>
    </el-dialog>

    <!-- 更换床位页面 -->
    <el-dialog title="更换床位" :visible.sync="changeBerthVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="changeBerth" :model="changeBerthForm">
        <el-form-item label="请选择床位">
          <el-select v-model="changeBerthForm.RY_bed">
            <el-option
              v-for="item in berth"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宿舍楼编码">
          <el-input v-model="changeBerthForm.SSL_number" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="区域编码">
          <el-input v-model="changeBerthForm.SSL_number" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="房间号">
          <el-input v-model="changeBerthForm.SS_number" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="人员工号">
          <el-input v-model="changeBerthForm.RY_degsonid" :disabled="true"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="changeBerthVisible = false">取消</el-button>
        <el-button type="primary" @click.native="changeBerthSubmit" :loading="changeBerthLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import {
  bed_select,
  bed_change,
  bed_update,
  bed_select_people,
  bed_article_select,
} from "../../api/bed";
// import { set } from "vue/types/umd";
export default {
  data() {
    return {
      data: [],
      goods: [],
      condition: {
        RY_degsonid: null,
        RY_name: null,
        RY_department: null,
        SS_number: null,
        page: 1,
      },
      tableData: [
        {
          AR_number: null,
          AR_name: null,
          SSL_name: null,
          SSL_number: null,
          SSL_cavenumber: null,
          SSL_floor: null,
          SS_number: null,
          SS_roomtype: null,
          RY_bed: null,
          RY_bedstatus: null,
          RY_name: null,
          RY_degsonid: null,
          RY_department: null,
          RY_phone: null,
        },
      ],
      bed_update: {
        RY_name: null,
        SS_number: null,
        RY_bed: null,
        RY_phone: null,
        // SQ_use: null,
        RY_remarks: null,
      },
      usegoods: [],
      bed_change: {},
      loading: false,
      newLoading: false,
      alterLoading: false,
      alterFormVisible: false,
      changeBerthVisible: false,
      berthTypeControl: false,
      alterTelControl: false,
      changeBerthForm: {
        AR_number: null,
        SSL_number: null,
        RY_number: null,
        RY_bed: null,
        RY_degsonid: null,
      },
      berth: [],
      changeBerthLoading: false,
      currentPage: 1,
      totalPage: 0,
    };
  },
  methods: {
    getData() {
      this.loading = true;
      bed_select({
        RY_degsonid: this.condition.RY_degsonid,
        RY_name: this.condition.RY_name,
        RY_department: this.condition.RY_department,
        SS_number: this.condition.SS_number,
        page: this.condition.page,
      }).then((res) => {
        this.loading = false;
        if (res) {
          this.tableData = res.data;
          this.totalPage = res.page;
        } else {
          this.talbleData = [];
          this.totalPage = 0;
          this.currentPage = 1;
        }
      });
    },
    //查询
    search() {
      this.condition.page = 1;
      this.getData();
    },
    //分页
    handleCurrentChange(val) {
      this.condition.page = val === undefined ? this.currentPage : val;
      this.getData();
    },
    selsChange: function (sels) {
      this.sels = sels;
    },

    //调整床位
    changeBerth(index, row) {
      this.changeBerthForm = Object.assign({}, row);
      this.changeBerthForm.RY_bed = null;
      // console.log(this.changeBerthForm);
      // this.changeBerthForm = {
      //   RY_degsonid: null,
      //   RY_bed: null,
      // };
      if (row.RY_name === "") {
        this.$message.error("该床位无人入住,不需要调整床位!");
      } else {
        this.changeBerthVisible = true;
        // console.log(row);
        bed_select_people({
          AR_number: row.AR_number,
          SSL_number: row.SSL_number,
          SS_number: row.SS_number,
        }).then((res) => {
          this.berth = [];
          if (res) {
            // this.berth = res.data;
            // res.data.forEach((item) => console.log(item.RY_bed));
            const list = res.data.map((item) => item.RY_bed);
            // console.log(list);
            for (let index = 0; index < list.length; index++) {
              this.berth.push({
                label: list[index],
                value: list[index],
              });
            }
          } else {
            this.dorm = [
              {
                value: "",
                label: "",
              },
            ];
          }
        });
      }
    },
    changeBerthSubmit() {
      // console.log(this.changeBerthForm.RY_bed);
      // 没有更换床位不提交
      // if (
      //   this.tableData[this.changeBerthForm.RY_degsonid].RY_degsonid ===
      //   this.changeBerthForm.RY_bed
      // ) {
      //   this.$message.warning("没有更换床位");
      // } else {
      {
        this.changeBerthLoading = true;
        console.log(this.changeBerthForm);
        bed_change({
          AR_number: this.changeBerthForm.AR_number,
          SSL_number: this.changeBerthForm.SSL_number,
          SS_number: this.changeBerthForm.SS_number,
          RY_degsonid: this.changeBerthForm.RY_degsonid,
          RY_bed: this.changeBerthForm.RY_bed,
        }).then((res) => {
          this.changeBerthLoading = false;
          if (res) {
            this.$message.success(res.msg);
          } else {
            this.$message.error(res.msg);
          }
          this.changeBerthVisible = false;
          this.search();
        });
      }
    },

    //编辑床位
    edit(index, row) {
      this.alterFormVisible = true;
      this.bed_update = Object.assign({}, row);
      //查询已领用和未领用物品存入usegoods中
      // console.log(this.bed_update);
      // const generateData = (_) => {
      bed_article_select({
        RY_name: this.bed_update.RY_name,
        SS_number: this.bed_update.SS_number,
        RY_bed: this.bed_update.RY_bed,
      }).then((res) => {
        if (res) {
          nouse = [];
          use = [];
          //每次调用该FUNCUTION 都要清除goods 和 data 的值
          this.goods = [];
          this.data = [];
          let nouse = res.article_noself;
          let use = res.article_self;
          // console.log(nouse);
          // console.log(use);
          //已领用
          for (let i = 0; i < nouse.length; i++) {
            this.data.push({
              key: nouse[i].SQ_use,
              label: nouse[i].SQ_useeffects,
              disabled: nouse[i].disabled,
            });
            this.goods.push(nouse[i].SQ_use);
          }
          //未领用
          for (let j = 0; j < use.length; j++) {
            this.data.push({
              key: use[j].SQ_use,
              label: use[j].SQ_useeffects,
              disabled: use[j].disabled,
            });
          }
        } else {
          this.data.push({
            key: "",
            label: "",
            disabled: "",
          });
        }
      });
      // };
      if (row.name !== "") {
        this.berthTypeControl = true;
        this.alterTelControl = false;
      } else {
        this.berthTypeControl = false;
        this.alterTelControl = true;
      }
    },
    alterSubmit(formName) {
      // console.log(this.bed_update);
      this.alterLoading = true;
      let roomgoods = this.goods.join("**");
      // console.log(roomgoods);
      bed_update({
        RY_name: this.bed_update.RY_name,
        RY_degsonid: this.bed_update.RY_degsonid,
        RY_phone: this.bed_update.RY_phone,
        SQ_use: roomgoods,
        RY_remarks: this.bed_update.RY_remarks,
      }).then((res) => {
        this.alterLoading = false;
        this.alterFormVisible = false;
        if (res) {
          this.$message({
            type: "success",
            message: res.msg,
          });
        } else {
          // this.alterLoading = false;
          // this.alterFormVisible = false;
          this.$message.error(res.msg);
        }
        this.search();
      });
    },
    outExcel() {
      // window.location.href =
      // "http://192.168.10.225:80/dormitoryA/building/Building/berthInfoExcelOut";
    },
  },
  mounted() {
    this.getData();
  },
};
</script>